<?php
// +----------------------------------------------------------------------
// | 二级域名/定制下单公用页面
// +----------------------------------------------------------------------
// | Author: TongBC <50249101@qq.com>
// +----------------------------------------------------------------------
// | Date  : 2015/05/13
// +----------------------------------------------------------------------
include_once("/var/www/html/new/com.inc.php");
include_once("/var/www/html/new/d/common/errBack.php");
include_once("/var/www/html/new/d/common/err_code.php");
include_once("/var/www/html/new/d/class/Sale.class.php");
include_once("/var/www/html/new/d/class/Member.php");
include_once("/var/www/html/new/d/class/ProductInfo.php");
include_once("/var/www/html/new/d/class/DisOrder.php");
include_once("/var/www/html/new/d/class/abc/DisOrder_c.php");// 个人扩展子类

$ac = $GLOBALS['_ac'];
$pw = $GLOBALS['_pw'];
$s = new SoapClient(null,array("location" => "http://localhost/open/openService/pft_insideMX.php","uri" => "www.16u.com?ac_16u=$ac|pw_16u=$pw|auth_16u=true"));


// 引入页面必须配置的参数 销售者ID、登陆账号、登陆账号ID
if(!isset($apply_did) || !isset($sourceID)) exit('非法引入');

$loginId = $_SESSION['sid'];// 站点登陆账号
$loginAccount = $_SESSION['account'];


$recv = $_POST;// 接收数据

// 基本信息验证
if(p_match($recv['ordername']))  
	exit("<script>alert('下单出错：取票人含有非法字符');javascript:history.go(-1);</script>");
if(p_match($recv['ordertel']) || strlen($recv['ordertel'])!=11)  
	//exit("下单出错：取票人电话格式不正确");
	exit("<script>alert('下单出错：取票人电话格式不正确');javascript:history.go(-1);</script>");
if(!$recv['idCard']) 
	exit("<script>alert('下单出错：请输入身份证号码');javascript:history.go(-1);</script>");
	//exit("下单出错：请输入身份证号码");

// 散客转换分销商身份
$sql = "select dtype from pft_member where id=$loginId and account='$loginAccount' limit 1";
$GLOBALS['le']->query($sql);
$GLOBALS['le']->fetch_assoc();
if($GLOBALS['le']->f('dtype')==5){
	$sql = "update pft_member set dtype=1 where id=$loginId and account='$loginAccount' limit 1";
	if(!$GLOBALS['le']->query($sql)) exit('下单失败:100001');
}

// 销售者与散客建立分销关系
$sql = "select id from pft_member_relationship where parent_id=$apply_did and son_id=$loginId and ship_type=0 limit 1";
$GLOBALS['le']->query($sql);
if(!$GLOBALS['le']->fetch_assoc()){
	$ins = "insert into pft_member_relationship set parent_id=$apply_did,son_id=$loginId,son_id_type=0,ship_type=0,rectime=now()";
	if(!$GLOBALS['le']->query($ins)) exit('下单失败:100002');;
}

// 基本数据模板
$tpl = array(
	'ordertel'   => $recv['ordertel'],
	'ordername'  => $recv['ordername'],
	'memo'       => $recv['memo'],
	'remoteorder'=> '',
	'sfz'        => $recv['idCard'],
	'sapply_did' => $apply_did,
	'contacttel' => $loginAccount,
	'apply_did'  => $sourceID,
	'paymode'    => 1
);
// print_r($recv);
// 数据处理
$data = array();
foreach($recv['pid'] as $key => $pid){
    if($recv['tnum'][$key]<1) continue; // 门票数量为0不购买
	$arr = $tpl;
	$arr['pid']        = $pid;
	$arr['tnum']       = $recv['tnum'][$key];
	$arr['begintime']  = $recv['begintime'][$key];
	$arr['leavetime']  = $recv['endtime'][$key];
	
	if($sourceID==0) $arr['apply_did']  = $recv['aid'][$key];
	
    // 餐饮类型或酒店类型
    if(isset($recv['ptype'][$key]) && ($recv['ptype'][$key]=='C'||$recv['ptype'][$key]=='G'||$recv['ptype'][$key]=='F')){
        $data[$key]= $arr;continue;
    }
	
	// 预定同一天当联票
	if(isset($data[$recv['lid'][$key]]) && ($recv['begintime'][$key]==$data[$recv['lid'][$key]]['begintime'])){ 
		$data[$recv['lid'][$key]]['c_pids'][$key]=$recv['tnum'][$key];
	}else{
		if(isset($data[$recv['lid'][$key]]) && ($recv['begintime'][$key]!=$data[$recv['lid'][$key]]['begintime'])){
			$data[$key]= $arr;continue;
		}
		$data[$recv['lid'][$key]]= $arr;
	}
    
}

// 下单权限配置及参数验证
$totalPrice = 0; 
$onlineTotal = 0;
foreach($data as $product){
    
	// 分销权限配置
    conf_permissions1($product['pid'],$product['apply_did'],$product['sapply_did'],$loginId);
    if(count($product['c_pids'])){
        foreach($product['c_pids'] as $pid_key => $tnum){
            conf_permissions1($pid_key,$product['apply_did'],$product['sapply_did'],$loginId);
        }
    }
	
	// 下单参数验证
    $pro = new ProductInfo($s,$product['pid'],$product['sapply_did']);
    $m = new Member($s,$_SESSION['sid']);
    $sOrder = new OrderSubmitYd($s, $pro, $m);
    try{
        $order_list[$product['pid']] = $sOrder->check($product, $product['sapply_did']);
		$totalPrice += $order_list['totalMoney'];
		if($order_list[$product['pid']]['paymode']!=4){// 计算在线支付总价
			$onlineTotal += $order_list[$product['pid']]['totalMoney'];
			if(count($order_list[$product['pid']]['c_infos'])){
				foreach($order_list[$product['pid']]['c_infos'] as $c_info){
					$onlineTotal += $c_info['totalMoney'];
				}
			}
		}
    }catch(Exception $e){
        exit('<script>alert("'.$e->getMessage().'")</script>');
    }
}

// if($sourceID==7136){
	// print_r($data);
	// exit;
// }
// print_r($data);
// exit;

// 开始下单
$order_s = array();
foreach($data as $product){
	// 下单参数验证
    $pro = new ProductInfo($s,$product['pid'],$product['sapply_did']);
    $m = new Member($s,$loginId);
    $sOrder = new OrderSubmitYd($s,$pro,$m);
	try{
        $result = $sOrder->order($product, $product['sapply_did']);
		$order_s[$product['pid']] = $result['orderNum'];
    }catch(Exception $e){
        exit('<script>alert("'.$e->getMessage().$e->getCode().'")</script>');
    }
}

// 没定义或者定义并且为true
if(!defined('EXECUTE')){
	
	// 清空购物车
	$sql="delete from uu_shoppingcart where fid=$loginId";
	$GLOBALS['le']->query($sql);

	// 合并订单号带进下单成功页面
	$paramOrder = $first_order = current($order_s);
	if(count($order_s)>1){// 多个订单
		$newOrder = 'st_'.$first_order;
		$subOrder = implode(',', $order_s);
		$sql="insert into `uu_unitpay` set new='$newOrder',old='$subOrder',status=0,tprice='$onlineTotal'";
		$paramOrder = $newOrder;
	}
	header('Location:/new/d/shops/gorderSuc_dev.html?ordernum='.$paramOrder.'&t='.$onlineTotal.'&suborder='.base64_encode($subOrder));
	exit;
}



// 产品权限购买
function conf_permissions1($pid,$aid,$mid,$memberID){
    
    if($aid==$mid){// 自己供应
        $check_sql = "select id,pids from pft_product_sale_list where fid=$memberID and aid=$aid limit 1";
        $GLOBALS['le']->query($check_sql);
        $GLOBALS['le']->fetch_assoc();
        $list_id = $GLOBALS['le']->f('id');
        if($list_id){
            if($GLOBALS['le']->f('pids')!='A'){
                $sql = "update pft_product_sale_list set pids='A' where id=$list_id limit 1";
                $GLOBALS['le']->query($sql);
			}
        }else{
            $sql = "insert into pft_product_sale_list set pids='A',fid=$memberID,aid=$aid";
            $GLOBALS['le']->query($sql);
        }
    }else{ // 转分销必须开启转分销才能在页面显示销售，因此pft_p_apply_evolute必有记录
		
		$check_sql = "select id,aids,sid,sourceid,lvl,active from pft_p_apply_evolute where pid=$pid and fid=$mid and sid=$aid and active=1 limit 1";
		$GLOBALS['le']->query($check_sql);
		if(($dbData = $GLOBALS['le']->fetch_assoc())){
			$check_sql = "select id,status from pft_p_apply_evolute where pid=$pid and fid=$memberID and sid=$mid limit 1";
			$GLOBALS['le']->query($check_sql);
			if($GLOBALS['le']->fetch_assoc()){// 登录购买的账号已经有记录
				if($GLOBALS['le']->f('status')==1){
					$id = $GLOBALS['le']->f('id');
					$upd_sql="update pft_p_apply_evolute set status=0 where id=$id and pid=$pid and fid=$memberID and sid=$mid limit 1";
                    $GLOBALS['le']->query($upd_sql);
				}
			}else{
				$aids_n  = $dbData['aids'].",$mid";
				$lvl_n = $dbData['lvl']+1;
				$sourceid = $dbData['sourceid'];
				$sql="insert into pft_p_apply_evolute set pid=$pid,fid=$memberID,sid=$mid,sourceid=$sourceid,aids='$aids_n',lvl=$lvl_n,active=0,rectime=now()";
				$GLOBALS['le']->query($sql);
			}
		}
    }
}
?>